package com.newcoin.chat.controller;

import com.newcoin.chat.beans.vo.ResultVo;
import com.newcoin.chat.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/upload")
@CrossOrigin(origins = "http://localhost:63343", maxAge = 3600)
public class UpLoadController {
    @Autowired
    private UploadService service;


    /**
     * 聊天图片语音上传
     *
     * @param file
     * @param type
     * @return
     */
    @PostMapping(value = "/uploadChatFile")
    public ResultVo<String> uploadChatFile(MultipartFile file, String type) {
        try {
            return service.uploadChatFile(file, type);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("上传失败");
            return resultVo;
        }
    }

    @PostMapping(value = "/uploadCircleOfFriendFile")
    public ResultVo<String> uploadCircleOfFriendFile(@RequestParam("files") MultipartFile[] files, String type) {
        try {
            return service.uploadCircleOfFriendFile(files, type);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统错误");
            return resultVo;
        }
    }

    /**
     * 修改头像上传图片
     * @param file
     * @return
     */
    @PostMapping(value = "/uploadHeadFile")
    public ResultVo<String> uploadHeadFile(MultipartFile file) {
        try {
            return service.uploadHeadFile(file);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统错误");
            return resultVo;
        }
    }
}
